bicycles = ["trek", "cannondale", "redline", "specialized"]
print(bicycles)

# 访问列表中的元素
print(bicycles[0])
print(bicycles[1])

print(bicycles[0].title())

# 修改列表元素
bicycles[2] = "giant"
print(bicycles)

# 添加元素
# 在列表末尾添加元素
bicycles.append("BMW")
print(bicycles)

# 在列表中插入元素
bicycles.insert(1, "Mercedes")
print(bicycles)

# 删除元素
del bicycles[0]
print(bicycles)

# 弹出栈尾元素
poped_bicycle = bicycles.pop()
print(bicycles)
print(poped_bicycle)

# 使用pop()方法删除列表中指定位置的元素
poped_bicycle = bicycles.pop(1)
print(bicycles)
print(poped_bicycle)

# 根据值删除元素
bicycles.remove("giant")
print(bicycles)


# 列表排序
# 使用sort()方法对列表进行永久排序
cars = ["bmw", "audi", "toyota", "subaru"]
cars.sort()
print(cars)

# 使用sorted()方法对列表进行临时排序
cars = ["bmw", "audi", "toyota", "subaru"]
print(sorted(cars))
print(cars)

# 使用reverse()方法对列表进行永久反向排序
cars.reverse()
print(cars)

# 获取列表长度
print(len(cars))

# 负数索引，从列表末尾开始的元素数量
print(cars[-1])
print(cars[-2])

# 列表的遍历
for bicycle in bicycles:
    print(bicycle.title() + " is a good bicycle.")
    print("I like " + bicycle.title() + ".")

print("I really like bicycles!")

# 使用range()函数
# range()函数的范围左闭右开
r = range(1, 5)
print(r)

for value in r:
    print(value)

# 使用range()函数创建数字列表
numbers = list(range(1, 10))
print(numbers)

# 带步进的range()函数
even_numbers = list(range(2, 11, 2))
print(even_numbers)

# 数值列表的简单计算
digits = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
print(min(digits), max(digits), sum(digits))

# 列表推导式
squares = [value ** 2 for value in range(1, 11)]
print(squares)

# 列表切片，index同样是左闭右开
players = ['charles', 'martina', 'michael', 'florence', 'eli']
print(players[0:3])
print(players[1:4])
# 从第二个元素到最后
print(players[2:])
# 获取最后两个元素
print(players[-2:])

# 遍历切片
for player in players[:3]:
    print(player.title())

# 复制列表
my_foods = ['pizza', 'falafel', 'carrot cake']
# 使用 [:] 复制整个列表
friend_foods = my_foods[:]

my_foods.append('cannoli')
friend_foods.append('ice cream')

print("My favorite foods are:")
print(my_foods)

print("\nMy friend's favorite foods are:")
print(friend_foods)

